package br.org.isvi.interpretador.portugol.types;

import br.org.isvi.interpretador.portugol.exception.SemanticErrorException;

public class RealSymbol extends NumberSymbol {

	public RealSymbol(Double value) {
		super(Kind.REAL, value);
	}
	
	public static RealSymbol valueOf(String val) {
		RealSymbol ret = null;
		
		try {
			ret  = new RealSymbol(Double.parseDouble(val.replace(',', '.')));
		}catch(NumberFormatException e) {
			throw new SemanticErrorException("Valor passado não é um numero real válido!");
		}
		
		return ret;
	}
	
	@Override
	public String toString() {
		return getValue().toString().replace('.', ',');
	}
	
	public boolean bigger(Symbol symb) {
		
		double value = ((Double)this.getValue()).intValue();
		double comp = 0;
		
		if(symb instanceof RealSymbol) {
			comp = ((Double)symb.getValue());
		} else {
			comp = ((Integer)symb.getValue());
		}
		
		return value > comp;
	}

	public boolean equals(Symbol symb) {
		
		double value = ((Double)this.getValue()).intValue();
		double comp = 0;
		
		if(symb instanceof RealSymbol) {
			comp = ((Double)symb.getValue());
		} else {
			comp = ((Integer)symb.getValue());
		}
		
		return value == comp;
	}

	public boolean smaller(Symbol symb) {
		double value = ((Double)this.getValue()).intValue();
		double comp = 0;
		
		if(symb instanceof RealSymbol) {
			comp = ((Double)symb.getValue());
		} else {
			comp = ((Integer)symb.getValue());
		}
		
		return value < comp;
	}
}
